<template>
  <div class="list">
    <div class="tab">
      <el-row>
        <div class="col">
          <el-col :span="6">
            <el-button type="primary" @click="handleAdd">添加房间</el-button>
          </el-col>
          <el-col :span="12">
            <div>
              <el-form :model="roomForm" :inline="true">
              <el-form-item style="margin-right: 0;">
                <el-input placeholder="请输入查询的房间id" style="width: 170px;" v-model="roomForm.id"></el-input>
                <el-input placeholder="请输入查询的房间型号" style="width: 170px;" v-model="roomForm.roomType"></el-input>
                <el-input placeholder="请输入查询的房间号" style="width: 150px;" v-model="roomForm.address"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-search" type="success" @click="onSubmit">查询</el-button>
              </el-form-item>
            </el-form>
            </div>
            
          </el-col>
          <el-col :span="6">
            <el-button type="info" @click="tranSort">{{ pricesSort }}</el-button>
          </el-col>
        </div>
        

      </el-row>
    </div>
    
    
    <el-table
    :data="rooms"
    
    style="width: 100%">
    <el-table-column
      prop="imageUrl"
      label="图片">
      <template slot-scope="scope">
         <img :src="scope.row.imageUrl"  width="170px" height="100px">
      </template>
    </el-table-column>
    <el-table-column
      prop="roomType"
      label="房间型号">
    </el-table-column>
    <el-table-column
      prop="bedsNumber"
      label="床的数量">
      <template slot-scope="scope">
            <div>{{ bedsNum(scope.row.bedsNumber) }}</div>
        </template>
    </el-table-column>
    <el-table-column
      prop="prices"
      label="价格">
    </el-table-column>
    <el-table-column
      prop="address"
      label="房间位置">
    </el-table-column>
    <el-table-column
      prop="count"
      label="房间数"
      width="180">
    </el-table-column>
    <el-table-column
      prop="createTime"
      label="添加时间">
    </el-table-column>
    <el-table-column
      prop="updateTime"
      label="修改时间">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button type="text" size="small"
        @click="handleAdd(scope.row)">修改</el-button>
        <el-button type="text" size="small" 
          @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pager">
    <el-pagination
    background
    layout="prev, pager, next,total"
    :total="total"
    :page-size="pageData.pageSize"
    @current-change="handlePage">
    </el-pagination>
  </div>
  
  </div>
</template>

<script>
import { delRoom, getRoomsAdmin} from "@/api/room";
import Cookie from 'js-cookie';
export default {
  name: "List",
  data(){
    return{
      rooms:[],
      pageData:{
        page:1,
        pageSize:5,
        state:1//0为顾客/1为管理员
      },
      roomForm:{
        roomType:'',
        id:'',
        address:'',
        prices:'cel'
      },
      modalType:0,//0为新增，1为修改
      total:0,
      pricesSort:'价格升序',
    }
  },

  methods:{
    handleAdd(row){{
      if (row.id) {
        this.modalType = 1
        Cookie.set('modalType',this.modalType)
        Cookie.set('row',JSON.stringify(row))
      }
      Cookie.set('modalType',this.modalType)
      this.$router.push({
        name:'Addroom',
      })

      
    }},

    handleDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRoom({params:{id}}).then((config) => {
          let {data} = config
          // console.log('删除的数据----',data);

              if(data.code ===1){
                this.$message.success(data.data)
              }else{
                this.$message.error(data.msg)
              }
              this.getList()
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
    },
    handlePage(val){
    // console.log(val,'val');
      this.pageData.page = val
      this.getList()
    },

    getList(){
      getRoomsAdmin({params:{...this.pageData,...this.roomForm}}).then(({data})=>{
        this.rooms = data.data.records
        this.total = Number(data.data.total)
        // console.log(data);
        
      }).catch(err => {
        console.log(err);
      })
    },
    onSubmit(){
      this.getList()
    },
    bedsNum(number){
      if(number == 1){
        return '单人床'
      }else if(number == 2){
        return '双人床'
      }else if(number == 4){
        return '两张双人床'
      }
    },
    tranSort(){
      
      if(this.pricesSort == '价格升序'){
        this.pricesSort = '取消升序'
        this.roomForm.prices = 'order'
      }else if(this.pricesSort == '取消升序'){
        this.roomForm.prices = 'cel'
        this.pricesSort = '价格升序'
      }
      this.getList()
    }
      
  },

  mounted() {
    this.getList()

  }
}
</script>

<style lang="less" scoped>

.list{
  height:  calc(100vh - 70px);
  width: calc(100vw - 150px);
  
  .tab{
    .col{
      display: flex;
      justify-content:center;
     
    }
  }
  .pager{
    position: absolute;
    bottom: 30px;
    // right: 20px;
    left: 50%;
  }
}


</style>
